package com.servlet;

import com.dao.CategoryDao;
import com.dao.PMationDao;
import com.dao.ReturnnDao;
import com.dao.impl.CategoryDaoImpl;
import com.dao.impl.PMationImpl;
import com.dao.impl.ReturnnDaoImpl;
import com.entry.Returnn;
import com.entry.Sales;
import com.entry.Zgoods;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.util.PageModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = "/PSales/return.do")
public class RetutnServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        HttpSession session = req.getSession();
        CategoryDao categoryDao = new CategoryDaoImpl();
        ReturnnDao returnnDao = new ReturnnDaoImpl();
        PMationDao pMationDao = new PMationImpl();
        String action = req.getParameter("action");
        JsonParser jParser = new JsonParser();
        if(action == null || "".equals(action)) {
            JsonObject jt = (JsonObject) jParser.parse(req.getParameter("c"));//构造JsonObject对象
            String cuname = jt.get("cuname").getAsString();
            String photo = jt.get("photo").getAsString();
            String reason = jt.get("reason").getAsString();
            String pname = jt.get("pname").getAsString();
            String gid = jt.get("gid").getAsString();
            try{
                long s = Long.valueOf(photo);
                int count = "".equals(jt.get("count").getAsString()) || jt.get("count").getAsString() == null?0:Integer.parseInt(jt.get("count").getAsString());
                if("".equals(cuname) || cuname == null || "".equals(photo) || photo == null ||
                        "".equals(reason) || reason == null || "".equals(pname) || pname == null ||
                        "".equals(gid) || gid == null || count == 0){
                    session.setAttribute("reMess","请完善信息");
                }else{
                    if(categoryDao.finId(gid) == null){
                        session.setAttribute("reMess","查询不到类别编号");
                    }else{
                        if(pMationDao.findName(pname) == null){
                            session.setAttribute("reMess","查询不到商品");
                        }else {
                            if(categoryDao.finId(gid).getGid().equals(pMationDao.findName(pname).getGid())) {
                                Zgoods zgoods = pMationDao.findName(pname);
                                int count1 = zgoods.getZcount() + count;
                                Returnn returnn = new Returnn();
                                returnn.setCuname(cuname);
                                returnn.setPhoto(photo);
                                returnn.setReason(reason);
                                returnn.setPname(pname);
                                returnn.setGid(gid);
                                returnn.setCountt(count);
                                if (returnnDao.add(returnn) != 0) {
                                    if(pMationDao.updateco(pname,count1) != 0){
                                        session.setAttribute("reMess", "添加成功");
                                    }else{
                                        session.setAttribute("reMess","数据更新失败");
                                    }
                                } else {
                                    session.setAttribute("reMess", "添加成功");
                                }
                            }else{
                                session.setAttribute("reMess", "商品与类别编号不对应");
                            }
                        }
                    }
                }
                resp.sendRedirect("./return.jsp");
            }catch (NumberFormatException e){
                session.setAttribute("reMess","电话或者数量输入异常");
                resp.sendRedirect("./return.jsp");
            }
        }else if("all".equals(action)){
            int pageSize = req.getParameter("pageSize") == null ? 5 : Integer.parseInt(req.getParameter("pageSize").toString());
            int pageNo = req.getParameter("pageNo") == null ? 1 : Integer.parseInt(req.getParameter("pageNo").toString());
            if(req.getParameter("pageNo") == null || req.getParameter("pageSize") == null){
                session.removeAttribute("c");
            }
            JsonObject jt = null;
            if(session.getAttribute("c") == null){
                jt = (JsonObject) jParser.parse(req.getParameter("c"));
            }else{
                jt = (JsonObject) jParser.parse(String.valueOf(session.getAttribute("c")));
            }
            String cuname = jt.get("cuname").getAsString();
            try{
                int stacount = "".equals(jt.get("stacount").getAsString()) || jt.get("stacount").getAsString() == null?0:Integer.parseInt(jt.get("stacount").getAsString());
                int endcount = "".equals(jt.get("endcount").getAsString()) || jt.get("endcount").getAsString() == null?0:Integer.parseInt(jt.get("endcount").getAsString());
                JsonObject language1=new JsonObject();
                language1.addProperty("cuname", cuname);
                language1.addProperty("stacount", String.valueOf(stacount));
                language1.addProperty("endcount", String.valueOf(endcount));
                PageModel<Returnn> pm  = returnnDao.find(pageSize,pageNo,cuname,stacount,endcount);
                pm.setPageNav(req.getRequestURI()+"?action=all&c="+language1);
                session.setAttribute("c",language1);
                session.setAttribute("ra",pm);
                resp.sendRedirect("./returall.jsp");
            }catch (NumberFormatException e){
                resp.sendRedirect("./returall.jsp");
            }
        }
    }
}
